<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>数据结构与算法之链表</title>
</head>

<body>
	<script>
		class Node {
			constructor(element) {
				this.element = element
				this.next = null
			}
		}
		class LinkedList {
			constructor() {
				this.count = 0
				this.head = null
			}
			push (element) {
				let node = new Node(element)
				if (!this.head) {
					this.head = node
				} else {
					let current = this.head
					while (current.next) {
						current = current.next
					}
					current.next = node
				}
				this.count++
				return this
			}
			toString () {
				if (this.isEmpty()) {
					return ''
				}
				let result = this.head.element;
				let current = this.head
				while (current.next) {
					current = current.next
					result = `${result},${current.element}`
				}
				return result
			}
			isEmpty () {
				return this.count === 0
			}
			insert (element, position) {
				if (position < 0 || position > this.count) {
					return false
				}
				let node = new Node(element)
				if (position === 0) {
					node.next = this.head
					this.head = node
				} else {
					let start = 0
					let result = this.head
					let parant = null
					while (start < position) {
						parant = result
						result = result.next
						start++
					}
					node.next = result
					parant.next = node
				}
				this.count++
				return true
			}
			get (index) {
				if (this.isEmpty() || index < 0 || index > this.count - 1) {
					return undefined
				}
				let start = 0
				let current = this.head
				while (start++ != index) {
					current = current.next
				}
				return current
			}
			// 返回元素在链表中的索引
			indexOf (element) {
				if (this.isEmpty()) {
					return undefined
				}
				let current = this.head
				// let start = 0
				// while(start++ < this.count) {
				// 	if(current.element === element) {
				// 		return start - 1
				// 	}else {
				// 		current = current.next
				// 	}
				// }
				// return -1
				for (let i = 0; i < this.count; i++) {
					if (current.element === element) {
						return i
					} else {
						current = current.next
					}
				}
				return -1
			}
			// 修改某一位置的元素	
			update (index, element) {
				if (this.isEmpty() || index > this.count - 1) {
					return false
				}
				let start = 0
				let current = this.head
				while (current) {
					if (start === index) {
						current.element = element
						// 注意这块逻辑
						return this
					} else {
						current = current.next
						start++
					}
				}
				return this
			}
			// 从列表重删除指定位置的项
			removeAt (index) {
				if (this.isEmpty() || index < 0 || index > this.count - 1) {
					return false
				}
				let current = this.head
				if (index === 0) {
					this.head = current.next
				} else {
					let start = 0
					let parant = null
					while (start++ < index) {
						parant = current
						current = current.next
					}
					parant.next = current.next
				}
				this.count--
				return this
			}
			// 从列表中删除一项
			remove (element) {
				if (this.isEmpty()) {
					return false
				}
				let current = this.head
				let parant = null
				try {
					while (current.element != element) {
						parant = current
						current = current.next
					}
					if (!parant) {
						this.head = current.next
					} else {
						parant.next = current.next
					}
					this.count--
					return this
				} catch (err) {
					return false
				}
			}
			size() {
				return this.count
			}
		}
		let linkedList = new LinkedList()
		linkedList.push('vue').push('javascript').push('css').push('html')
		linkedList.insert('react', 4)
		// console.log(linkedList.get(4))
		// console.log(linkedList.indexOf('react'))
		// console.log(linkedList.update(1,'uniapp').update(0,'微信小程序'))
		// console.log(linkedList.removeAt(0))
		// console.log(linkedList.removeAt(1))
		// console.log(linkedList.removeAt(3))
		// console.log(linkedList.remove('react').remove('vue').remove('javascript'))
		console.log(linkedList.remove('vue'))
	</script>
</body>

</html>
